package fhp.hlhj.giantfold.customView

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import fhp.hlhj.giantfold.R
import kotlinx.android.synthetic.main.shake_red_dialog.*
import java.text.DecimalFormat

/**
 * Created by Administrator on 2018\2\6 0006.
 */
class ShakeRedDialog(context:Context,private val num:String):Dialog(context, R.style.custom_dialog) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.shake_red_dialog)


        btOpenRed.setOnClickListener {
            btOpenRed.isClickable=false
            val alpha1 = ObjectAnimator.ofFloat(btOpenRed, "alpha", 1f, 0f)
            val alpha2 = ObjectAnimator.ofFloat(btOpenRed, "scaleX", 0f, 3f)
            val alpha3 = ObjectAnimator.ofFloat(btOpenRed, "scaleY", 0f, 3f)
            val set = AnimatorSet()
            set.play(alpha1).with(alpha2).with(alpha3)
            set.duration = 1200
            set.start()

            set.addListener(object :Animator.AnimatorListener{
                override fun onAnimationRepeat(p0: Animator?) {
                }

                override fun onAnimationEnd(p0: Animator?) {
                    tvResult.visibility=View.VISIBLE
                    tvResult.text="利率红包：${DecimalFormat("0.000").format(num.toDouble()).toString()}%"
                    val animator = ObjectAnimator.ofFloat(tvResult, "alpha", 0f, 1f).setDuration(1000)
                    animator.addListener(object :Animator.AnimatorListener{
                        override fun onAnimationRepeat(p0: Animator?) {
                        }

                        override fun onAnimationEnd(p0: Animator?) {
                            dismiss()
                        }

                        override fun onAnimationCancel(p0: Animator?) {
                        }

                        override fun onAnimationStart(p0: Animator?) {
                        }
                    })
                    animator.start()
                }

                override fun onAnimationCancel(p0: Animator?) {
                }

                override fun onAnimationStart(p0: Animator?) {
                }

            })
        }
    }
}